package com.xgame.server.dbtool;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.xgame.server.db.mapper.EquipMapper;
import com.xgame.server.db.model.Equip;
import com.xgame.server.netty.proto.Game;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class EquipCsTool {
    @Resource
    private EquipMapper equipMapper;

    //插入背包物品
    public Integer insertEquip(Game.EquipCs data) throws Exception {
        //1.首先删除该id已经有的物品
        delteEquipByAccount(data.getAccount());

        //2.插入新的物品表
        Integer result = new Integer(0);

        for (int i = 0; i < data.getArrayList().size(); i++) {
            Game.EquipOneCs one = data.getArray(i);
            Equip bag = new Equip();
            bag.setAccount(data.getAccount());
            bag.setPlayerId(data.getPlayerID());
            bag.setGuid(one.getGuid());
            bag.setItemId(one.getItemId());
            bag.setEatId(one.getEatID());
            bag.setEatLevel(one.getEatLevel());
            bag.setEatExp(new Long(one.getEatExp()));
            bag.setQuality(one.getQuality());
            bag.setHasSeal(one.getHasSeal());
            bag.setLastSealTime(one.getLastSealTime());
            bag.setIsLock(one.getIsLock());
            String lst = "";

            //洗练
            List<Integer> items = one.getQualityListList();
            int length = items.size();
            for (int j = 0; j < length; j++) {
                if (j == length - 1) {
                    lst = lst + items.get(j);
                } else {
                    lst = lst + items.get(j) + "^";
                }
            }

            bag.setQualityList(lst);

            //封印
            lst = "";
            List<Game.SealValue> seals = one.getSealListList();
            length = seals.size();
            for (int j = 0; j < length; j++) {
                if (j == length - 1) {
                    lst = lst + seals.get(j).getSealId() + "*" + seals.get(j).getSealValue();
                } else {
                    lst = lst + seals.get(j).getSealId() + "*" + seals.get(j).getSealValue() + "^";
                }
            }

            bag.setSealList(lst);
            bag.setDemonLv(one.getDemonLv());
            bag.setDemonExp(new Long(one.getDemonExp()));

            result += equipMapper.insert(bag);
        }

        return result;
    }

    //删除所有玩家的物品
    public Integer delteEquipByAccount(String account) throws Exception {
        EntityWrapper<Equip> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("account", account);
        return equipMapper.delete(entityWrapper);
    }

    //删除某玩家的某一些物品
    public Integer deleteEquipByAccount(String account, List<Integer> guids) throws Exception {
        EntityWrapper<Equip> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("account", account);
        entityWrapper.in("guid", guids);

        return equipMapper.delete(entityWrapper);
    }

    //查询某玩家的所有物品
    public List<Equip> selectEquipByAccount(String account) throws Exception {
        EntityWrapper<Equip> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("account", account);

        return equipMapper.selectList(entityWrapper);
    }
}
